﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Oridea.Data.Fetching {
	public class DictionaryFetcher<T, TKey, TObject> : IFetcher<Dictionary<TKey, TObject>> {
		private readonly IQueryable<T> _query;
		private readonly Func<T, TKey> _keySelector;
		private readonly Func<T, TObject> _objectSelector;

		public DictionaryFetcher(IQueryable<T> query, Func<T, TKey> keySelector, Func<T, TObject> objectSelector) {
			if (query == null) {
				throw new ArgumentNullException("query");
			}
			if (keySelector == null) {
				throw new ArgumentNullException("keySelector");
			}
			if (objectSelector == null) {
				throw new ArgumentNullException("objectSelector");
			}

			_query = query;
			_keySelector = keySelector;
			_objectSelector = objectSelector;
		}

		public Dictionary<TKey, TObject> Fetch() {
			return _query.ToDictionary(_keySelector, _objectSelector);
		}

		public int Count() {
			return _query.Count();
		}
	}
}
